<?php
/**
 * @author    Mindaugas Valinskis <m.valinskis@gmail.com>
 * @copyright 2012 Min Vin
 * @version   $Id: $
 */

/**
 * Greet class.
 *
 * This class is used to retrieve an expression of direct address to person.
 */
class Greet
{

  /**
   * Rules.
   *
   * @var array $_config
   */
  private static $_config = NULL;

  /**
   * Language.
   *
   * @var string $_language
   */
  private static $_language = NULL;


  /**
   * Load config and initialize it.
   *
   * @param string $lang Language.
   *
   * @return void
   */
  public static function init($lang='lt')
  {
    require_once 'config.php';

    self::$_config   = $conf;
    self::$_language = $lang;

  }//end init()


  /**
   * Load config and initialize it.
   *
   * @param string $name Name to greet.
   * @param string $lang Language (optional).
   *
   * @return string
   */
  public static function me($name, $lang=NULL)
  {
    if (isset(self::$_config) === FALSE) {
      if (isset($lang) === FALSE) {
        $lang = 'lt';
      }//end if

      self::init($lang);
    }//end if

    $name = trim($name);
    $temp = $name;
    $conf = self::$_config[self::$_language];

    if (isset($lang) === $lang) {
      $conf = self::$_config[$lang];
    }//end if

    while (empty($temp) === FALSE) {
      if (isset($conf[$temp]) === TRUE) {
        return mb_substr($name, 0, -mb_strlen($temp)).$conf[$temp];
      }//end if

      $temp = mb_substr($temp, 1);
    }//end while

    return $name;

  }//end me()


}

?>